﻿/// <reference path="../../../fly.common/fly/fly.js" />
!function () {
    var $ = fly;
    window.indexPath = '../'
    $(function () {
        var docTitle = $('#doc-title');
        var frameWrap = $('#frame-wrap');
        var frame = $("#frame-wrap iframe");
        var aDown = $('#a-down');
        var id = $.getQuery('id');
        var code = $.getQuery('code');
        var title = $.getQuery('name') || '';
        var inFrame = window.parent != window

        try {
            title = decodeURIComponent(title)
        } catch (e) { }
        document.title = title;
        docTitle.text(title).attr('title', title);
        frame[0].src = id ? fly.box.ajaxUrl('SpaceFile', 'Open', "id=" + id) : "../open.ashx/" + code;
        aDown.attr('href', "../down.ashx?" + (code ? "c=" + code : "id=" + id))

        function autoHeight() {
            var fDocu = frame[0].contentWindow.document
            var h = Math.max(fDocu.documentElement.scrollHeight, fDocu.body.scrollHeight)
            window.frameElement.style.height = (h + 50) + "px";
        }

        function changeState(loaded) {
            $.doc[loaded ? 'addClass' : 'removeClass']('loaded');
            if (loaded) {
                clearInterval(checkLoadHandle)
                if (inFrame) {
                    aDown.attr("target", "_blank")
                    autoHeight()
                    setTimeout(autoHeight, 2000);
                    $.doc.addClass("in-frame")
                }
            }
        }

        //        function stateChange() {

        //            t.innerText = frame[0].document.readyState + ' ' + frame[0].contentWindow.location.href;
        //        }

        //debugger
        //        var t = document.getElementById('doc-title')
        //        setInterval(function () {
        //            var t = document.getElementById('doc-title')
        //            var state = frame[0].contentWindow.document.readyState
        ////            t.innerText = state;
        ////            t.innerText = frame[0].contentWindow.history.length + ' ' + state + ' ' + frame[0].contentWindow.location.href;

        //            frame[0].contentWindow.document.onDOMContentLoaded = function () {
        //                t.innerText= frame[0].contentWindow.document.readyState
        //            }

        //        }, 10);


        frame[0].onreadystatechange = function () {
            var state = frame[0].readyState
            changeState(state == "interactive" || state == 'complete')
        }

        if (!$.browser.isIE) {
            var checkLoadHandle = 0
            var checkLoadHandle = setInterval(function () {
                if (frame[0].contentWindow.document.body) {
                    changeState(true);
                }
            }, 50);

            frame[0].onload = function () {
                changeState(true)
            }
        }
    });
} ();